using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public enum CameraLook
{
    Right,
    Top,
    Forward,
    Orig,
}

public class CameraController : MonoBehaviour
{
    public static CameraLook cameralook;
    Coroutine cameraMove;
    Coroutine cameraDelay;
    Transform spring;
    PlayerController player;

    Vector3 OriDir;

    private void Awake()
    {
        spring= transform.parent;
        cameralook = CameraLook.Orig;
        player = FindObjectOfType<PlayerController>();

        MessageManager.BindAction(MsgKey.ClickRight, CameraMoveToRight);
        MessageManager.BindAction(MsgKey.ClickUp, CameraMoveToTop);
        MessageManager.BindAction(MsgKey.ClickForward, CameraMoveToForward);
        MessageManager.BindAction(MsgKey.ClickOrig, CameraMoveToOrig);

        OriDir = spring.eulerAngles;
    }

    void CameraMoveToRight()
    {
        StopCameraMove();
        cameraMove = StartCoroutine(CameraMove(new Vector3(0,-90,0), CameraLook.Right));
    }

    void CameraMoveToForward()
    {
        StopCameraMove();
        cameraMove = StartCoroutine(CameraMove(new Vector3(0, 0, 0), CameraLook.Forward));
    }

    void CameraMoveToTop()
    {
        StopCameraMove();
        cameraMove = StartCoroutine(CameraMove(new Vector3(90, 0, 0), CameraLook.Top));
    }

    void CameraMoveToOrig()
    {
        StopCameraMove();
        cameraMove = StartCoroutine(CameraMove(OriDir, CameraLook.Orig));
    }

    void StopCameraMove()
    {
        if (cameraMove != null)
        {
            StopCoroutine(cameraMove);
            StopCoroutine(cameraDelay);
            cameraMove = null;
            cameraDelay = null;
        }
    }

    IEnumerator CameraMove(Vector3 targetAngle, CameraLook look)
    {
        MessageManager.Run(MsgKey.FindStander);
        cameraDelay = StartCoroutine(CameraMoving(targetAngle, look));
        LevelManager.instance.cameraMoving = true;

        if (look == CameraLook.Orig)
        {
            MessageManager.Run(MsgKey.MoveToOrig);
            yield return new WaitForFixedUpdate();
            MessageManager.Run(MsgKey.MoveableMoveToOrig);
        }
        Quaternion tarQua = Quaternion.Euler(targetAngle);
        while (true)
        {
            spring.rotation = Quaternion.Lerp(spring.rotation, tarQua, Time.deltaTime * 6);
            yield return null;
        }
    }

    IEnumerator CameraMoving(Vector3 targetAngle, CameraLook look)
    {
        MessageManager.Run(MsgKey.NavmeshEnable, false);
        yield return new WaitForSeconds(1f);
        StopCameraMove();
        cameralook = look;
        spring.rotation = Quaternion.Euler(targetAngle);
        switch (look)
        {
            case CameraLook.Top:
                MessageManager.Run(MsgKey.MoveToUp);
                break;

            case CameraLook.Right:
                MessageManager.Run(MsgKey.MoveToRight);
                break;

            case CameraLook.Forward:
                MessageManager.Run(MsgKey.MoveToForward);
                break;
        }

        LevelManager.instance.BuildNavMesh();
        MessageManager.Run(MsgKey.NavmeshEnable, true);
        LevelManager.instance.cameraMoving = false;
    }
}
